//
//  OrderSuccessViewController.m
//  EasyToTesco2.0
//
//  Created by knew on 16/10/13.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "OrderSuccessViewController.h"
#import "BaseNavigationController.h"
#import "CoastOrderViewController.h"
#import "HomeViewController.h"
#import "OilOrderViewController.h"
#import "GoodsOrderViewController.h"
#import "RechargeRecordController.h"

@interface OrderSuccessViewController ()

@end

@implementation OrderSuccessViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    NSLog(@"%@,%@",self.orderNumber,self.creatOrderTime);
    
//    self.title = @"支付成功";
    
    [self setUpOrderSuccessNavigationBar];
    
    [self addOrderSuccessView];
}

- (void)setUpOrderSuccessNavigationBar {
    
    self.navigationController.navigationBar.barTintColor = DEFAULT_COLOR;
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"jiantou"] highImage:[UIImage imageNamed:@"jiantou"] target:self action:@selector(backDissmissToPre) forControlEvents:UIControlEventTouchUpInside title:nil];
    self.navigationItem.leftBarButtonItem.imageInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setText:@"支付成功"];
    customLab.font = [UIFont boldSystemFontOfSize:18];
    self.navigationItem.titleView = customLab;
    
}

- (void)backDissmissToPre {
    
    if ([self.orderType isEqualToString:@"3"]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
        NSArray *controllerArray = self.navigationController.viewControllers;
        
        [self.navigationController popToViewController:[controllerArray objectAtIndex:1]  animated:YES];
    }
}

- (void)didClickOrderButtonGoToMyOrderController {
    NSLog(@"我的订单");
    
    switch (self.orderType.integerValue) {
        case 1:
            [self pushOrderControllerWithOrderController:[CoastOrderViewController new]];
            break;
            
        case 2:
            [self pushOrderControllerWithOrderController:[[OilOrderViewController alloc] init]];
            break;
            
        case 3:
            [self pushOrderControllerWithOrderController:[GoodsOrderViewController new]];
            break;
            
        case 4:
            [self pushOrderControllerWithOrderController:   [RechargeRecordController new]];
            break;
            
        default:
            break;
    }
    
}

- (void)addOrderSuccessView {
    
    UIImageView *LoGoImageView = [[UIImageView alloc] init];
    LoGoImageView.image = [UIImage imageNamed:@"icon_logo"];
    [self.view addSubview:LoGoImageView];
    
    [LoGoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view.mas_top).offset(HEIGHT/10);
        make.width.mas_equalTo(WIDTH/5*1.25);
        make.height.mas_equalTo(self.view.width/5);
    }];
    
    UIImageView *successImageView = [[UIImageView alloc] init];
    successImageView.image = [UIImage imageNamed:@"zhifuwancheng"];
    [self.view addSubview:successImageView];
    
    [successImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(LoGoImageView.mas_left).with.offset(-4);
        make.top.equalTo(LoGoImageView.mas_bottom).offset(16);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
    
    UILabel *successLabel = [[UILabel alloc] init];
    successLabel.text = @"支付成功";
    successLabel.textColor = DEFAULT_COLOR;
//    successLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:16];
    successLabel.font = [UIFont systemFontOfSize:20];
    [self.view addSubview:successLabel];
    
    [successLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(successImageView.mas_right).offset(4);
        make.right.equalTo(self.view.mas_right).offset(4);
        make.top.equalTo(successImageView.mas_top);
        make.centerY.equalTo(successImageView.mas_centerY);
    }];
    
    UILabel *firstLabel = [[UILabel alloc] init];
    firstLabel.text = [NSString stringWithFormat:@"订单号：%@",self.orderNumber];
    firstLabel.font = [UIFont systemFontOfSize:14];
    firstLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:firstLabel];
    
    [firstLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(successLabel.mas_bottom).with.offset(16);
        make.height.mas_equalTo(25);
    }];
    
    UILabel *secondLabel = [[UILabel alloc] init];
    secondLabel.text = [NSString stringWithFormat:@"成交时间：%@",self.creatOrderTime];
    secondLabel.font = [UIFont systemFontOfSize:14];
    secondLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:secondLabel];
    
    [secondLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(firstLabel.mas_bottom);
        make.height.mas_equalTo(25);
    }];
    
    UILabel *thiredLabel = [[UILabel alloc] init];
    NSMutableAttributedString * str = [[NSMutableAttributedString alloc]initWithString:@"支付成功后，您可以在我的订单中查看订单进程"];
    NSDictionary * dict1 = @{
                             NSForegroundColorAttributeName:[UIColor redColor],
                             NSUnderlineStyleAttributeName:@"1"};
    [str addAttributes:dict1 range:NSMakeRange(10, 4)];
    thiredLabel.attributedText = str;
    thiredLabel.font = [UIFont systemFontOfSize:14];
    thiredLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:thiredLabel];
    
    [thiredLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(secondLabel.mas_bottom);
        make.height.mas_equalTo(25);
    }];
    
    UIButton *button = [[UIButton alloc] init];
    [button addTarget:self action:@selector(didClickOrderButtonGoToMyOrderController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(thiredLabel.mas_centerX).with.offset(20);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(thiredLabel.mas_height);
        make.top.equalTo(thiredLabel.mas_top);
    }];
    
    UILabel *lastLabel = [[UILabel alloc] init];
    lastLabel.text = @"该商品由亿乐购物吧提供";
    lastLabel.font = [UIFont systemFontOfSize:14];
    lastLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lastLabel];
    
    [lastLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.top.equalTo(thiredLabel.mas_bottom);
        make.height.mas_equalTo(25);
    }];
    
    UIButton *successButton = [[UIButton alloc] init];
    [successButton setBackgroundColor:DEFAULT_COLOR];
    [successButton setTitle:@"完成" forState:UIControlStateNormal];
    [successButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    successButton.layer.cornerRadius = 6;
    successButton.layer.masksToBounds = YES;
    successButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    successButton.layer.borderWidth = 0.5;
    [successButton addTarget:self action:@selector(didClickSuccessButtonBackToPre) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:successButton];
    
    [successButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastLabel.mas_bottom).offset(30);
        make.height.mas_equalTo(40);
        make.centerX.equalTo(self.view.mas_centerX);
        make.width.mas_equalTo(WIDTH * 0.85);
    }];
}

- (void)didClickSuccessButtonBackToPre {
    if ([self.orderType isEqualToString:@"3"]) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else {
        NSArray *controllerArray = self.navigationController.viewControllers;
        
        [self.navigationController popToViewController:[controllerArray objectAtIndex:1]  animated:YES];
    }
}

- (void)pushOrderControllerWithOrderController:(UIViewController *)orderController {
    
//    BaseNavigationController *base = [[BaseNavigationController alloc] initWithRootViewController:orderController];
////    [self presentViewController:nav animated:YES completion:nil];
//    [base pushForm:base pushToAndHidden:orderController];

    [self.navigationController pushForm:self.navigationController pushToAndHidden:orderController];
    
}

@end
